package com.hzcx.app.simplechat.ui.moment.adapter;

import android.graphics.drawable.Drawable;
import android.text.SpannableString;
import android.text.style.ImageSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chad.library.adapter.base.util.MultiTypeDelegate;
import com.hzcx.app.simplechat.R;
import com.hzcx.app.simplechat.ui.moment.bean.MomentBean;
import com.hzcx.app.simplechat.ui.moment.bean.MomentDyImagesBean;
import com.hzcx.app.simplechat.ui.moment.util.CenterAlignImageSpan;
import com.hzcx.app.simplechat.util.dp.DpUtils;
import com.hzcx.app.simplechat.util.empty.EmptyUtils;
import com.hzcx.app.simplechat.util.glide.GlideUtils;
import com.hzcx.app.simplechat.util.log.LogUtils;
import com.hzcx.app.simplechat.view.CommentTextView;
import com.hzcx.app.simplechat.view.FabulousTextView;
import com.liyi.grid.AutoGridView;
import com.liyi.grid.adapter.SimpleAutoGridAdapter;
import com.shuyu.gsyvideoplayer.GSYVideoManager;
import com.shuyu.gsyvideoplayer.builder.GSYVideoOptionBuilder;
import com.shuyu.gsyvideoplayer.listener.GSYSampleCallBack;
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer;

import java.util.ArrayList;
import java.util.List;

public class MomentAdapter extends BaseQuickAdapter<MomentBean, BaseViewHolder> {

    private static final int TYPE_MOMENT = 1;
    private static final int TYPE_ADVERTISEMENT = 2;

    private static final int MOMENT = R.layout.rv_item_moment;
    private static final int ADVERTISMENT = R.layout.rv_item_moment_advertisement;


    public static final String TAG = "ListNormalAdapter22";

    private int videoMaxWidth = 850;


    public MomentAdapter(@Nullable List<MomentBean> data) {
        super(data);
        setMultiTypeDelegate(new MultiTypeDelegate<MomentBean>() {
            @Override
            protected int getItemType(MomentBean momentBean) {
                if (momentBean.getType() == 2) {
                    return TYPE_ADVERTISEMENT;
                } else {
                    return TYPE_MOMENT;
                }
            }
        });

        getMultiTypeDelegate()
                .registerItemType(TYPE_MOMENT, MOMENT)
                .registerItemType(TYPE_ADVERTISEMENT, ADVERTISMENT);
    }


    @Override
    protected void convert(BaseViewHolder helper, MomentBean item) {
        if (item.getType() == TYPE_ADVERTISEMENT) {
            // 广告
            helper.setText(R.id.tv_nick_name, item.getAd_name());
            GlideUtils.loadImg(mContext, item.getAd_logo(), helper.getView(R.id.iv_head));
            helper.setText(R.id.tv_date, item.getCreatetime());
            setImgs(helper.getView(R.id.rv_img), item.getDy_images());
            FabulousTextView tvFabulous = helper.getView(R.id.tv_fabulous);
            tvFabulous.setIsFabulous(item.getGreatstate());
            tvFabulous.setFabulousNum(item.getGreatcount());
            CommentTextView tvComment = helper.getView(R.id.tv_commont);
            tvComment.setIsComment(item.getReviewstate());
            tvComment.setFabulousNum(item.getReviewcount());
            helper.setText(R.id.tv_address, item.getLocation_address());

            // 设置内容
            if (EmptyUtils.isEmpty(item.getLinkurl())) {
                // 如果没有链接，直接设置内容
                helper.setText(R.id.tv_content, item.getMaincontent());
            } else {
                // 有链接，添加网页链接字样
                String str = item.getMaincontent().substring(0, item.getSubscript()) + "网页链接" + item.getMaincontent().substring(item.getSubscript());
                TextView tvContent = helper.getView(R.id.tv_content);
                SpannableString sps = new SpannableString(str);
                Drawable drawable = mContext.getResources().getDrawable(R.mipmap.ic_icon_moment_http_text);
                drawable.setBounds(0, 0, DpUtils.dp2px(mContext, 80), DpUtils.dp2px(mContext, 15));
                //居中对齐imageSpan
                CenterAlignImageSpan imageSpan = new CenterAlignImageSpan(drawable);
                sps.setSpan(imageSpan, item.getSubscript(), item.getSubscript() + 4, ImageSpan.ALIGN_BASELINE);
                tvContent.setText(sps);
            }

            // 添加视频播放
            if (EmptyUtils.isNotEmpty(item.getVideofile())) {
                StandardGSYVideoPlayer videoPlayer = helper.getView(R.id.videoPlayer);
                // 动态设置播放器宽高
                ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) videoPlayer.getLayoutParams();

                int w;
                if (item.getVideo_cover_width() > videoMaxWidth) {
                    w = videoMaxWidth;
                } else {
                    w = item.getVideo_cover_width();
                }
                params.width = w;
                params.height = item.getVideo_cover_height();
                videoPlayer.setLayoutParams(params);
                videoPlayer.setVisibility(View.VISIBLE);

                ImageView imageView = new ImageView(mContext);
                GlideUtils.loadImg(mContext, item.getVideo_cover(), imageView);

                videoPlayer.setThumbImageView(imageView);
                videoPlayer.setIsTouchWiget(false);
                videoPlayer.setUp(item.getVideofile(), true, "");
                videoPlayer.setRotateViewAuto(true);
                videoPlayer.setTag(TAG);
                videoPlayer.setReleaseWhenLossAudio(true);
                videoPlayer.setVideoAllCallBack(new GSYSampleCallBack() {
                    @Override
                    public void onPrepared(String url, Object... objects) {
                        super.onPrepared(url, objects);
                        GSYVideoManager.instance().setNeedMute(true);
                    }

                    @Override
                    public void onQuitFullscreen(String url, Object... objects) {
                        super.onQuitFullscreen(url, objects);
                        GSYVideoManager.instance().setNeedMute(true);
                    }

                    @Override
                    public void onEnterFullscreen(String url, Object... objects) {
                        super.onEnterFullscreen(url, objects);
                        GSYVideoManager.instance().setNeedMute(false);
                    }
                });

                videoPlayer.getBackButton().setVisibility(View.GONE);
                videoPlayer.getFullscreenButton().setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        videoPlayer.startWindowFullscreen(mContext, false, true);
                    }
                });

                AutoGridView rvImg = helper.getView(R.id.rv_img);
                rvImg.setVisibility(View.GONE);
                if (helper.getAdapterPosition() == 1) {
                    videoPlayer.startPlayLogic();
                }
            }

            helper.addOnClickListener(R.id.tv_fabulous, R.id.tv_commont, R.id.iv_more, R.id.tv_content, R.id.tv_web_url);
        } else {
            // 动态
            if (EmptyUtils.isNotEmpty(item.getRemarks_name())) {
                helper.setText(R.id.tv_nick_name, item.getRemarks_name());
            } else {
                helper.setText(R.id.tv_nick_name, item.getNickname());
            }
            GlideUtils.loadImg(mContext, item.getAvatar(), helper.getView(R.id.iv_head));
            helper.setText(R.id.tv_date, item.getCreatetime());
            setImgs(helper.getView(R.id.rv_img), item.getDy_images());
            FabulousTextView tvFabulous = helper.getView(R.id.tv_fabulous);
            tvFabulous.setIsFabulous(item.getGreatstate());
            tvFabulous.setFabulousNum(item.getGreatcount());
            CommentTextView tvComment = helper.getView(R.id.tv_commont);
            tvComment.setIsComment(item.getReviewstate());
            tvComment.setFabulousNum(item.getReviewcount());
            helper.setText(R.id.tv_address, item.getLocation_address());

            // 设置内容
            if (EmptyUtils.isEmpty(item.getLinkurl())) {
                // 如果没有链接，直接设置内容
                helper.setText(R.id.tv_content, item.getMaincontent());
            } else {
                try {
                    // 有链接，添加网页链接字样
                    String str = item.getMaincontent().substring(0, item.getSubscript()) + "网页链接" + item.getMaincontent().substring(item.getSubscript());
                    TextView tvContent = helper.getView(R.id.tv_content);
                    SpannableString sps = new SpannableString(str);
                    Drawable drawable = mContext.getResources().getDrawable(R.mipmap.ic_icon_moment_http_text);
                    drawable.setBounds(0, 0, DpUtils.dp2px(mContext, 80), DpUtils.dp2px(mContext, 15));
                    //居中对齐imageSpan
                    CenterAlignImageSpan imageSpan = new CenterAlignImageSpan(drawable);
                    sps.setSpan(imageSpan, item.getSubscript(), item.getSubscript() + 4, ImageSpan.ALIGN_BASELINE);
                    tvContent.setText(sps);
                } catch (Exception e) {
                    e.printStackTrace();
                    helper.setText(R.id.tv_content, item.getMaincontent());
                }
            }
            helper.addOnClickListener(R.id.iv_head, R.id.tv_fabulous, R.id.tv_commont, R.id.iv_more, R.id.tv_content);
        }
    }

    /**
     * 设置展示的 图片
     *
     * @param rvImgs
     */
    private void setImgs(AutoGridView rvImgs, List<MomentDyImagesBean> imgUrls) {

        List<String> imgs = new ArrayList<>();
        List<String> imgsBig = new ArrayList<>();

        if (imgUrls != null && imgUrls.size() > 0) {
            for (MomentDyImagesBean bean : imgUrls) {
                imgs.add(bean.getImgurl());
                imgsBig.add(bean.getReal_imgurl());
            }
        }

        SimpleAutoGridAdapter adapter = new SimpleAutoGridAdapter();
        adapter.setSource(imgs);
        adapter.setImageLoader((position, source, view, viewType) -> GlideUtils.loadImgSimple(mContext, source.toString(), view));
        rvImgs.setOnItemClickListener((i, view) -> {
            if (callback != null) {
                callback.onItemClick(i, imgsBig, rvImgs);
            }
        });
        rvImgs.setGridOneHpercent(0.65f);
        rvImgs.setGridOneWpercent(0.65f);
        rvImgs.setAdapter(adapter);
        if (EmptyUtils.isEmpty(imgs) || imgs.size() <= 0) {
            rvImgs.setVisibility(View.GONE);
            return;
        } else {
            rvImgs.setVisibility(View.VISIBLE);
        }
    }

    private OnItemClickCallback callback;

    public void setOnItemClickCallback(OnItemClickCallback clickCallback) {
        this.callback = clickCallback;
    }

    public interface OnItemClickCallback {
        void onItemClick(int position, List<String> list, ViewGroup viewGroup);
    }
}
